package com.lechuang.web.exception;

import cn.hutool.core.util.StrUtil;
import com.lechuang.common.result.Result;
import com.lechuang.common.result.ResultEnum;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * <p>
 *   全局异常捕获
 * </p>
 * @author liaoningbo
 * @since 2023/6/7
*/
@ControllerAdvice(basePackages = "com.lechuang")
@Log4j2
public class GlobalExceptionHandler {

    /**
     * <p>
     *   业务异常
     * </p>
     * @author liaoningbo
     * @since 2023/6/7
    */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result<String> businessException(BusinessException e) {
        log.error("业务异常：", e);
        return Result.error(ResultEnum.BUSINESS_ERROR.getCode(), e.getMsg());
    }


    /**
     * <p>
     *   系统异常
     * </p>
     * @author liaoningbo
     * @since 2023/6/7
    */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<String> error(Exception e) {
        log.error("异常：", e);
        return Result.error(ResultEnum.ERROR.getCode(), e.getMessage());
    }

    /**
     * <p>
     *   运行时异常
     * </p>
     * @author liaoningbo
     * @since 2023/6/7
    */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Result<String> runtimeException(RuntimeException e) {
        log.error("异常：", e);
        return Result.error(ResultEnum.ERROR.getCode(), e.getMessage());
    }

    /**
     * <p>
     *   参数异常
     * </p>
     * @author liaoningbo
     * @since 2023/6/7
    */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public Result<String> validatedException(ConstraintViolationException e) {
        log.error("参数异常：", e);
        String message = e.getMessage();
        if(StrUtil.isNotBlank(message)){
            String[] split = message.split(":");
            if(split.length >= 2){
                message = split[1];
            }
        }
        return Result.error(ResultEnum.PARAM_ERROR.getCode(), message.trim());
    }

    /**
     * <p>
     *   参数异常
     * </p>
     * @author liaoningbo
     * @since 2023/6/7
    */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Result<String> methodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error("参数异常：", e);
        FieldError fieldError = e.getBindingResult().getFieldError();
        String msg = null;
        if(fieldError != null){
            msg  =  fieldError.getDefaultMessage();
        }
        return Result.error(ResultEnum.PARAM_ERROR.getCode(), msg);
    }

    /**
     * <p>
     *   参数异常
     * </p>
     * @author liaoningbo
     * @since 2023/6/7
    */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public Result<String> missingServletRequestParameterException(MissingServletRequestParameterException e) {
        log.error("参数异常：", e);
        return Result.error(ResultEnum.PARAM_ERROR.getCode(), e.getParameterName() + "不能为空");
    }
}
